package top.jidanmian.iot.device.interaction;

import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import top.jidanmian.iot.device.dao.DeviceEntityRepository;
import top.jidanmian.iot.device.entity.DeviceEntity;
import top.jidanmian.iot.device.service.DeviceService;

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;


@RestController
public class DeviceController {
    @Resource
    private DeviceEntityRepository repository;
    private DeviceService deviceService;

    @GetMapping("/devices")
    public List<DeviceEntity> listDevice() {
        List<DeviceEntity> devices = repository.findAll();
        return devices;
    }

    @GetMapping("/device")
    public DeviceVO getDeviceModel(@RequestBody DeviceVO device) {
        Optional<DeviceEntity> entity = repository.findById(device.getId());
       
        if (entity.isPresent()) {
            device = new DeviceVO(entity.get());
        }
        
        return device;
    }

    @PostMapping("/device")
    public DeviceVO postMethodName(@RequestBody DeviceVO device) {
        DeviceVO deviceVO = deviceService.updateStatus(device.getId(), device.getStatus());

        return deviceVO;
    }
    
}
